<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11/011
 * Time: 16:20
 */

namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Session;

class Course extends Controller
{
    public function list()//显示首页方法
    {
        $this->checkLogin();
        $session=Session::get('admin');
        $result=Db::table('course')
            ->alias('a')
            ->join('cub w','a.cub_id = w.cub_id')
            ->where('adminname',$session['adminname'])
            ->select();
        // dump($result);
        $count = count($result);    //获取课程条数
        $this->assign('count', $count);
        $this->assign('result',$result);
        return $this->fetch();
    }

    public function delect()//删除课程方法
    {
        $this->checkLogin();
        $request=input('course_id');
        if (Db::table("course")->delete($request)){
            $this->success("删除成功");
        }
        else{
            $this->error("删除失败");
        }
    }

    public function addlist()//课程添加页面方法
    {
        $this->checkLogin();
        $session=Session::get('admin');
        $cub=Db::table('cub')->where('adminname',$session['adminname'])->select();
        // dump($cub);
        $this->assign('cub',$cub);
        return $this->fetch('addlist');
    }

    public function add()//添加处理方法
    {
        $this->checkLogin();
        $request=input('');
        $sql=Db::table('course')->insert($request);
        if ($sql){
            $this->success('增加成功','admin/course/list');
        }
        else{
            $this->error('增加失败','admin/course/list');
        }
    }

    public function search () {
        $this->checkLogin();
        $session=Session::get('admin');
        $content = input('content');
        $result=Db::table('course')
            ->alias('a')
            ->join('cub w','a.cub_id = w.cub_id')
            ->where('adminname',$session['adminname'])
            ->where('course_name','like','%'.$content.'%')
            ->select();
        // dump($result);
        $count = count($result);    //获取课程条数
        $this->assign('count', $count);
        $this->assign('result',$result);
        return $this->fetch('list');
    }
}